﻿using JT808Protocol.Abstracts;
using JT808Protocol.Models;
using JT808Protocol.Models.BodyParserStrategy;
using JT808Protocol.Models.BodyParserStrategy.DeviceParams;
using System;
using System.Linq;

namespace JT808Protocol.Handlers
{
    /// <summary>
    /// 终端鉴权消息处理
    /// </summary>
    public class AuthenticationHandler : MsgHandlerBase
    {
        public override EnumMessageType[] handleTypes => new EnumMessageType[] { EnumMessageType.终端鉴权 };

        public override JT808Package[] Execute(JT808Package reqPkg)
        {
            if (!handleTypes.Contains(reqPkg.Header.MessageType)) return null;

            //终端注册后每次在与平台建立连接后，应立即进行鉴权。鉴权成功前终端不得发送其它消息。
            //终端通过发送终端鉴权消息进行鉴权，平台回复平台通用应答消息。

            #region 终端参数设置

            var bodyDPS = new PrDeviceParamSet_0x8103();

            //位置汇报策略
            bodyDPS.AddParam(new DpDwordUint(0x20, 0));

            //位置汇报方案
            bodyDPS.AddParam(new DpDwordUint(0x21, 0));

            //缺省时间汇报间隔
            var iTimeAccOff = 120; //Acc关
            var iTimeAccOn = 18; //Acc开

            bodyDPS.AddParam(new DpDwordUint(0x22, (uint)iTimeAccOff)); //未登录上报间隔
            bodyDPS.AddParam(new DpDwordUint(0x27, (uint)iTimeAccOff)); //Acc开上报间隔
            bodyDPS.AddParam(new DpDwordUint(0x29, (uint)iTimeAccOn));  //Acc关上报间隔

            #endregion 终端参数设置

            return MultiResult(reqPkg, EnumResult.成功,
                (EnumMessageType.设置终端参数, bodyDPS));
        }
    }
}